//
//  LogingViewController.m
//  PatientApp
//
//  Created by chirui on 2017/5/16.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "LogingViewController.h"
#import "ForgetCodeViewController.h"
#import "RegistUserViewController.h"
#import "TabBarVc.h"



@interface LogingViewController ()
@property (weak, nonatomic) IBOutlet UIView *PhoneView;
@property (weak, nonatomic) IBOutlet UITextField *phoneText;
@property (weak, nonatomic) IBOutlet UIView *passwordView;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UIButton *logingBtn;

@end

@implementation LogingViewController

-(void)viewWillDisappear:(BOOL)animated {
    

    self.navigationController.navigationBarHidden =NO;
}

-(void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBarHidden = YES; //隐藏
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
//    self.navigationItem.titleView = [TopLabel withLabelWithLabelText:@"登录"];
    
//    self.PhoneView.layer.cornerRadius = 22.5f;
//    self.PhoneView.layer.masksToBounds = YES;
//    self.PhoneView.layer.borderWidth = 0.5;
//    self.PhoneView.layer.borderColor = [UIColor grayColor].CGColor;
//    
//    self.passwordView.layer.cornerRadius = 22.5f;
//    self.passwordView.layer.masksToBounds = YES;
//    self.passwordView.layer.borderWidth = 0.5;
//    self.passwordView.layer.borderColor = [UIColor grayColor].CGColor;
    
    self.logingBtn.layer.cornerRadius = 22.5f;
    self.logingBtn.layer.masksToBounds = YES;
    
}
- (IBAction)backClick:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
   
}

- (IBAction)forgetClick:(UIButton *)sender {
    
    ForgetCodeViewController * forget =[[ForgetCodeViewController alloc]init];
//    forget.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:forget animated:YES];
    

}
- (IBAction)logingBtnClick:(UIButton *)sender {
    
    if ([SHJudgeTools valiMobile:self.phoneText.text] != nil) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.yOffset = 100.f;
        hud.labelText = [SHJudgeTools valiMobile:self.phoneText.text];
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:0.5];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [hud removeFromSuperview];
        });
        return;
    }else if (self.phoneText.text.length!=11) {
        
        [SHJudgeTools MBProgressHUDPromptWith:@"手机号码不正确" On:self.view];
        return;
        
    }else if(_passwordText.text.length<1){
        
        [SHJudgeTools MBProgressHUDPromptWith:@"密码不能为空" On:self.view];
        return;
        
    }else{
    
     [self logingNetWorking];
    }
    
   
}
- (IBAction)reginBtnClick:(UIButton *)sender {
    
    RegistUserViewController * regist =[[RegistUserViewController alloc]init];
    [self.navigationController pushViewController:regist animated:YES];
}

//登录
-(void)logingNetWorking{
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",NetWorkAddress_api,user_login_api];
    NSDictionary * parameter = @{@"phone":_phoneText.text,
                                 @"password":_passwordText.text,
                                };
    NSLog(@"param ==%@",parameter);
    
    SHuiMBhubNoTimeStr(@"正在登录");
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:parameter  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"code"] intValue]==200) {
            SHuiMBhudTextTimeStr(@"登录成功");
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                
                NSString * token= responseObject[@"datas"][@"token"];
                USER_DEFAULTSet(token, @"token");
                USER_DEFAULTSet(@"OK", @"enter");
                USER_DEFAULTSet(@"OK", @"tokenVerify");
                
                NSString * userId= responseObject[@"datas"][@"user_id"];
                USER_DEFAULTSet(userId, @"user_id");
               
                USER_DEFAULTSet(responseObject[@"datas"][@"phone"], @"phone");
                
                USER_DEFAULTSet(token, @"token");
                
                NSLog(@"user_id == %@",USER_DEFAULTGet(@"user_id"));
                 NSLog(@"token == %@",USER_DEFAULTGet(@"token"))
                ;
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW , (1 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
                    
                    [self.navigationController popViewControllerAnimated:YES];

                    });
            });

            
        } else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"请检查您的网络!");
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
